package com.ds.web.controller.applet;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ds.common.annotation.Log;
import com.ds.common.core.controller.BaseController;
import com.ds.common.core.domain.AjaxResult;
import com.ds.common.enums.BusinessType;
import com.ds.applet.domain.MiniLeague;
import com.ds.applet.service.IMiniLeagueService;
import com.ds.common.utils.poi.ExcelUtil;
import com.ds.common.core.page.TableDataInfo;

/**
 * 加盟商Controller
 *
 * @author liucan
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/applet/league")
public class MiniLeagueController extends BaseController {
    @Resource
    private IMiniLeagueService miniLeagueService;

    /**
     * 查询加盟商列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MiniLeague miniLeague, HttpServletRequest request) {
        startPage();
        miniLeague.setToApplet(toApplet(request));
        List<MiniLeague> list = miniLeagueService.selectMiniLeagueList(miniLeague);
        return getDataTable(list);
    }

    /**
     * 登录加盟商
     */
    @GetMapping("/login")
    public AjaxResult login(MiniLeague miniLeague) {
        System.out.println("miniLeague=="+miniLeague);
        List<MiniLeague> list = miniLeagueService.login(miniLeague);
        System.out.println("list=="+list);
        return AjaxResult.success(list);
    }

    /**
     * 新增加盟商
     */
    @Log(title = "加盟商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniLeague miniLeague, HttpServletRequest request) {
        miniLeague.setToApplet(toApplet(request));
        return miniLeagueService.insertMiniLeague(miniLeague);
    }

    /**
     * 修改加盟商
     */
    @Log(title = "加盟商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniLeague miniLeague, HttpServletRequest request) {
        miniLeague.setToApplet(toApplet(request));
        return toAjax(miniLeagueService.updateMiniLeague(miniLeague));
    }

    /**
     * 删除加盟商
     */
    @Log(title = "加盟商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniLeagueService.deleteMiniLeagueByIds(ids));
    }


    /**
     * 导出加盟商列表
     */
    @Log(title = "加盟商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniLeague miniLeague) {
        List<MiniLeague> list = miniLeagueService.selectMiniLeagueList(miniLeague);
        ExcelUtil<MiniLeague> util = new ExcelUtil<MiniLeague>(MiniLeague.class);
        util.exportExcel(response, list, "加盟商数据");
    }

    /**
     * 获取加盟商详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(miniLeagueService.selectMiniLeagueById(id));
    }

    /**
     * 重复
     */
    public String toApplet(HttpServletRequest request) {
        return request.getHeader("toApplet").equals("admin") ? "" : request.getHeader("toApplet");
    }

    /**
     * 分页获取加盟商的信息
     * @param miniLeague
     * @param request
     * @return
     */
    @GetMapping("listNoPage")
    public TableDataInfo listNoPageLeague(MiniLeague miniLeague, HttpServletRequest request) {
//        startPage();
        miniLeague.setToApplet(toApplet(request));
        List<MiniLeague> list = miniLeagueService.selectMiniLeagueList(miniLeague);
        return getDataTable(list);
    }

}
